/*
 * (C) Copyright 2014-2016, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.array

import org.jheaps.AddressableHeap
import org.jheaps.array.DaryArrayAddressableHeap.Companion.heapify
import org.jheaps.tree.AbstractAddressableHeapTest
import org.junit.Assert
import org.junit.Test

class D3DaryArrayAddressableHeapTest : AbstractAddressableHeapTest() {
    override fun createHeap(): AddressableHeap<Int, Void> {
        return DaryArrayAddressableHeap(3)
    }

    override fun createHeap(comparator: Comparator<Int?>?): AddressableHeap<Int, Void> {
        return DaryArrayAddressableHeap(3, comparator)
    }

    override fun createHeapWithStringValues(): AddressableHeap<Int, String> {
        return DaryArrayAddressableHeap(3)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testWrongD() {
        DaryArrayAddressableHeap<Int, Void>(1)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegal1() {
        DaryArrayAddressableHeap<Int, String>(3, -5)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegal2() {
        DaryArrayAddressableHeap<Int, String>(3, Int.MAX_VALUE)
    }

    @Test
    fun testHandlesIterator() {
        val a = arrayOfNulls<Int>(SIZE)
        for (i in 0 until SIZE) {
            a[i] = SIZE - i
        }

        val heap = heapify<Int?, Void>(3, a, null)
        val it: Iterator<AddressableHeap.Handle<Int?, Void>> = heap.handlesIterator()

        val found: MutableSet<Int?> = HashSet()
        while (it.hasNext()) {
            val handle = it.next()
            Assert.assertTrue(found.add(handle.key))
        }
        Assert.assertEquals(SIZE.toLong(), found.size.toLong())
    }

    @Test(expected = NoSuchElementException::class)
    fun testHandlesIteratorTooMuch() {
        val a = arrayOfNulls<Int>(SIZE)
        for (i in 0 until SIZE) {
            a[i] = SIZE - i
        }

        val heap = heapify<Int?, Void>(3, a, null)
        val it: Iterator<AddressableHeap.Handle<Int?, Void>> = heap.handlesIterator()
        while (it.hasNext()) {
            it.next()
        }
        it.next()
    }

   /* @Test(expected = UnsupportedOperationException::class)
    fun testNoRemove() {
        val a = arrayOfNulls<Int>(SIZE)
        for (i in 0 until SIZE) {
            a[i] = SIZE - i
        }
        val heap = heapify<Int?, Void>(3, a, null)
        val it: Iterator<AddressableHeap.Handle<Int?, Void>> = heap.handlesIterator()
        it.remove()
    }*/
}
